local XMPQT = XLGetGlobal("XMPQT")
XMPQT.Output.Info("=================================================")
XMPQT.Output.Title("测试项目：验证程序签名") -- 格式要unicode

local tab = nil --配置表
local nCount = 0
local tablepath = "\\\\192.168.13.159\\share\\kankan\\XMPQuickTest\\lua\\CheckSign.lua"

function Result()
	XMPQT.Continue(nCount == 0)
end

function CheckTrust(filename,timestamptb)
	XMPQT.Output.Info("文件路径:"..filename)
	local bRet,filetimetable =  XMPQT.API:Call("CheckTrust",filename)
	
	if bRet == 1 then
		if filetimetable["OgnizationName"] == nil or tab["organization"] ~= filetimetable["OgnizationName"] then
			XMPQT.Output.Error("获取数字签名,公司名称出错")
			return false
		end
			
		if filetimetable["year"] == 0 and filetimetable["month"] == 0 and filetimetable["day"] == 0 then
			XMPQT.Output.Error("获取数字签名,时间戳出错")
			return false
		end
		
		local timestamp = os.time({year = filetimetable["year"], month = filetimetable["month"], day = filetimetable["day"],hour = filetimetable["hour"],minute = filetimetable["minute"],second = filetimetable["second"]})
		if timestamptb.imin < timestamp and timestamp < timestamptb.imax then
			XMPQT.Output.Info("时间戳:"..filetimetable["year"].."/"..filetimetable["month"].."/"..filetimetable["day"].." "..filetimetable["hour"]..":"..filetimetable["minute"]..":"..filetimetable["second"])
		else
			--与当前时间不一样警告
			XMPQT.Output.Warning("时间戳:"..filetimetable["year"].."/"..filetimetable["month"].."/"..filetimetable["day"].." "..filetimetable["hour"]..":"..filetimetable["minute"]..":"..filetimetable["second"])
		end
	else
		if   bRet== 0 then
			XMPQT.Output.Error("验签失败:"..filename)
		else 
			if bRet == -1 then
				XMPQT.Output.Error("验签失败:"..filename.."验签环境初始化失败")
			else
				if bRet == -2 then
					XMPQT.Output.Error("验签失败:"..filename.."文件不存在")
				end
			end
		end
		XMPQT.Output.Error("获取数字签名出错.")
		return false
	
	end

	return bRet
	
end

function CopyToTemp(srcpathname,name)
	local xmpqttmpfolder =  XMPQT.API:Call("ExpandEnvironmentStrings", "%temp%\\xmpqt\\"..name) 
	XMPQT.Output.Info("正在拷贝："..srcpathname.."->"..xmpqttmpfolder)
	XMPQT.API:Call("CopyFile",srcpathname,xmpqttmpfolder,0)
	
	return xmpqttmpfolder

end

local luapath = CopyToTemp("\\\\192.168.13.159\\share\\kankan\\XMPQuickTest\\lua\\CheckSign.lua","CheckSign.lua")
local ft = XLLoadModule(luapath)

if type(ft) == "table" and type(ft.GetSubTable) == "function" then --配置信息校验
	tab = ft.GetSubTable()
	local setuppath = tab["setup"]
	if setuppath == nil then -- 安装包路径
		XMPQT.Output.Error("获取安装包路径出错")
		nCount = nCount +1
		Result() 
		return
		
	else  ---
		-- 获取安装包
		if tab["AutoSetup"]== 1 then
			XMPQT.Output.Info("安装包路径："..setuppath)
			local settuppathname = CopyToTemp(setuppath,"settup.exe")
			if not CheckTrust(settuppathname,tab["timestamp"]) then
				XMPQT.Output.Error("安装包验签失败.")
			return
			
			end
			--静默安装
			XMPQT.Output.Info("正在安装："..settuppathname)
			XMPQT.API:Call("SHExec",settuppathname,"/s",0xFFFFFFFF )
			XMPQT.API:Call("Sleep",4000)
		end
		
		if type(tab["files"]) == "table" then
		
			for i=1,#tab["files"] do
				if type(tab["files"][i]) == "table" then
					local folder = tab["files"][i]["folder"]
					XMPQT.Output.Info("文件夹路径："..folder)
					
					folder = XMPQT.API:Call("ExpandEnvironmentStrings", tostring(folder))
				
					if XMPQT.API:Call("PathFileExists", folder)  then --and type(tab["files"][i]["filename"]) == table
						for n = 1,#tab["files"][i]["filename"] do
							local filename = folder.."\\"..tab["files"][i]["filename"][n]
							local timestamp = tab["timestamp"]
					
							if type(timestamp) == "table" then
								local ret = CheckTrust(filename,timestamp)
								if not ret then
									nCount = nCount +1 
									--return 
								end
							else
								XMPQT.Output.Error("时间戳配置获取异常")
								nCount = nCount +1 
								return
							end

						end
						
						XMPQT.API:Call("Sleep",2000) --
						
					else
						XMPQT.Output.Error("文件夹不存在："..folder)
						nCount = nCount +1
						--return
					end
				end
			end
		end
	end
else
	XMPQT.Output.Error("获取安装包路径出错")
	nCount = nCount +1
	Result() 
	return 
end
Result() 












